﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using EncuentraloSilverlight.Models;
using System.Collections.Generic;
using System.Data.Linq;

namespace EncuentraloSilverlight.Web.Models
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service2
    {
        // Categories
        [OperationContract]
        public List<Category> FindAllCategories()
        {
            CategoryRepository cat = new CategoryRepository();
            return cat.FindAllCategories().ToList();
        }
        [OperationContract]
        public List<Category> FindCategoryByName(string name)
        {
            CategoryRepository cat = new CategoryRepository();
            return cat.FindCategoryByName(name).ToList();
        }
        [OperationContract]
        public Category GetCategory(int id)
        {
            CategoryRepository cat = new CategoryRepository();
            return cat.GetCategory(id);
        }
        [OperationContract]
        public void AddCategory(Category category)
        {
            CategoryRepository cat = new CategoryRepository();
            cat.Add(category);
        }
        [OperationContract]
        public void DeleteCategory(Category category)
        {
            CategoryRepository cat = new CategoryRepository();
            cat.Delete(category);
        }


        // Addresses
        [OperationContract]
        public List<Address> FindAllAddress()
        {
            AddressRepository adr = new AddressRepository();
            return adr.FindAllAddress().ToList();
        }
        [OperationContract]
        public void AddAddress(Address address)
        {
            AddressRepository adr = new AddressRepository();
            adr.Add(address);
        }
        [OperationContract]
        public void DeleteAddress(Address address)
        {
            AddressRepository adr = new AddressRepository();
            adr.Delete(address);
        }


        // Products
        [OperationContract]
        public List<Product> FindAllProducts()
        {
            ProductRepository prod = new ProductRepository();
            return prod.FindAllProducts().ToList();
        }
        [OperationContract]
        public List<Product> FindProductByName(string name)
        {
            ProductRepository prod = new ProductRepository();
            return prod.FindProductByName(name).ToList();
        }
        [OperationContract]
        public Product GetProduct(int id)
        {
            ProductRepository prod = new ProductRepository();
            return prod.GetProduct(id);
        }
        [OperationContract]
        public void AddProduct(Product product)
        {
            ProductRepository prod = new ProductRepository();
            prod.Add(product);
        }
        [OperationContract]
        public void DeleteProduct(Product product)
        {
            ProductRepository prod = new ProductRepository();
            prod.Delete(product);
        }
        [OperationContract]
        public void ModifyProduct(int product_id, Product product)
        {
            ProductRepository prod = new ProductRepository();
            prod.ModifyProduct(product_id, product);
        }

        // Searches
        [OperationContract]
        public List<Search> FindAllSearches()
        {
            SearchRepository search = new SearchRepository();
            return search.FindAllSearches().ToList();
        }
        [OperationContract]
        public List<Search> FindSearchByParameter(string name)
        {
            SearchRepository search = new SearchRepository();
            return search.FindSearchByParameter(name).ToList();

        }
        [OperationContract]
        public List<Search> FindSearchByDate(string date)
        {
            SearchRepository search = new SearchRepository();
            return search.FindSearchByDate(date).ToList();
        }
        [OperationContract]
        public List<Search> FindSearchByType(int n)
        {
            SearchRepository search = new SearchRepository();
            return search.FindSearchByType(n).ToList();
        }
        [OperationContract]
        public Search GetSearch(int id)
        {
            SearchRepository search = new SearchRepository();
            return search.GetSearch(id);
        }
        [OperationContract]
        public void AddSearch(Search search)
        {
            SearchRepository sear = new SearchRepository();
            sear.Add(search);
        }
        [OperationContract]
        public void DeleteSearch(Search search)
        {
            SearchRepository sear = new SearchRepository();
            sear.Delete(search);
        }

        // Sellers
        [OperationContract]
        public List<Seller> FindAllSellers()
        {
            SellerRepository sell = new SellerRepository();
            return sell.FindAllSellers().ToList();
        }
        [OperationContract]
        public List<Seller> FindSellerFromName(string Name)
        {
            SellerRepository sell = new SellerRepository();
            return sell.FindSellerFromName(Name).ToList();
        }
        [OperationContract]
        public Seller GetSeller(int id)
        {
            SellerRepository sell = new SellerRepository();
            return sell.GetSeller(id);
        }
        [OperationContract]
        public void AddSeller(Seller seller)
        {
            SellerRepository sell = new SellerRepository();
            sell.Add(seller);
        }
        [OperationContract]
        public void DeleteSeller(Seller seller)
        {
            SellerRepository sell = new SellerRepository();
            sell.Delete(seller);
        }
        [OperationContract]
        public List<Product> GetAllProductOfSellersByCommune(string commune)
        {
            SellerRepository sell = new SellerRepository();
            return sell.GetAllProductsOfSellersByCommune(commune).ToList();
        }
        [OperationContract]
        public List<Seller> GetAllSellersByCommune(string commune)
        {
            SellerRepository sell = new SellerRepository();
            return sell.GetAllSellersByCommune(commune).ToList();
        }
        [OperationContract]
        public List<string> GetAllCommunesBySellers()
        {
            SellerRepository sell = new SellerRepository();
            return sell.GetAllCommunesBySellers().ToList();
        }
        // Users
        [OperationContract]
        public List<User> FindAllUsers()
        {
            
            EncuentraloDataBaseModelDataContext db = new EncuentraloDataBaseModelDataContext();

            DataLoadOptions options = new DataLoadOptions();
            options.LoadWith<User>(k => k.Addresses);
            options.LoadWith<User>(k => k.Searches);
            db.LoadOptions = options;

            return db.Users.ToList();
        }
        [OperationContract]
        public List<User> FindUsersFromUsername(string parameter)
        {
            UserRepository use = new UserRepository();
            return use.FindUsersFromUsername(parameter).ToList();
        }
        [OperationContract]
        public void AddUser(User user)
        {
            UserRepository use = new UserRepository();
            use.AddUser(user);
        }
        [OperationContract]
        public void DeleteUser(User user)
        {
            UserRepository use = new UserRepository();
            use.DeleteUser(user);
        }
        [OperationContract]
        public void EditUserName(User user, string name)
        {
            UserRepository use = new UserRepository();
            use.EditUserName(user, name);

        }
        [OperationContract]
        public void EditUserAddress(User user, Address address)
        {
            UserRepository use = new UserRepository();
            use.EditUserAddress(user, address);
        }
        [OperationContract]
        public void AddSearchfromUser(User user, Search search)
        {
            UserRepository use = new UserRepository();
            use.AddSearchfromUser(user, search);
        }

        // Add more operations here and mark them with [OperationContract]
    }
}
